<template>
  <!-- 经销商 事业部、代码、名称  搜索 -->
  <div>
    <!-- 搜索 -->
    <TopFilter :list="topFilterInit" :cols="4" labelWidth="90" @filterChange="changeHandle"></TopFilter>
    <!-- table -->
    <FilterTable
      height="250"
      ref="listModal"
      :columns="columns"
      :selectionType="'single'"
      :fetchapi="fetchParams"
      :params="flterValue"
      :datakey="'records'"
      :onColumnsChange="columns => this.columns = columns"
      columnsRef="projectOperating_case_departmentReport_dealerMadal_01"      
      
    ></FilterTable>
    <div class="dialogBottmView">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button @click="saveDialog" type="primary">{{submit}}</el-button>
    </div>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { region } from '@/api/cms-common/region';
import { getRegion } from '@/api/cms-common/dealerdata/index';
export default {
  mixins: [dictionary, region],
  props: {
    submit: {
      type: String,
      default: '确定'
    }
  },
  data() {
    return {
      visible: false,
      visibleDrawer: false,
      infoId: null,
      topFilterInit: this.topFilterInitFun(),
      flterValue: {}, //参数
      columns: this.creatHeader(),
      fetchParams: null //ipa 接口
    };
  },
  methods: {
    //搜索数据
    topFilterInitFun() {
      return [
        { type: 'INPUT', label: '经销商代码', fieldName: 'field2', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '经销商简称', fieldName: 'field3', placeholder: '请输入', initialValue: '' },
        { type: 'SELECT', label: '事业部', fieldName: 'field1', placeholder: '请选择', initialValue: '', itemList: [] },
        {
          type: 'SELECT',
          label: '所在省',
          fieldName: 'customerProvince',
          placeholder: '请选择',
          initialValue: '',
          change: value => {
            this.initCity(value);
          },
          itemList: [],
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'blur'
            }
          ]
        },
        {
          type: 'SELECT',
          label: '所在市',
          fieldName: 'customerCity',
          placeholder: '请选择',
          initialValue: '',
          itemList: [],
          change: value => {
            this.initTown(value);
          }
        },
        {
          type: 'SELECT',
          label: '所在县/区',
          fieldName: 'customerDistrict',
          placeholder: '请选择',
          initialValue: '',
          itemList: []
        }
      ];
    },
    //获取事业部下拉列表数据
    async getDeptData() {
      const res = await getRegion();
      if (res.resultCode === 200) {
        this.topFilterInit[2].itemList = res.data.map(x => ({ value: x.orgId, text: x.orgName }));
      }
    },
    //初始化所在省份
    initProvince() {
      let pl = this.createRegionList();
      this.topFilterInit[3].itemList = pl;
    },
    //初始化所在市数据
    initCity(provinceId) {
      //取省数据
      let rd = this.getRegionData();
      this.cityData = rd[provinceId].children;
      let cl = this.createSubRegionList(this.cityData);
      this.topFilterInit[4].itemList = cl;
    },
    //初始化所在县市数据
    initTown(cityId) {
      let td = this.cityData[cityId].children;
      let tl = this.createSubRegionList(td);
      this.topFilterInit[5].itemList = tl;
    },
    //搜索获取数据
    changeHandle(val) {
      this.flterValue = val;
      console.log(val);
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        { title: '公司全称', dataIndex: 'field0', sorter: true },
        { title: '经销商代码', dataIndex: 'field1', sorter: true },
        { title: '经销商简称', dataIndex: 'field2', sorter: true },
        { title: '经销商类型', dataIndex: 'field3', sorter: true },
        { title: '经销商级别', dataIndex: 'field4', sorter: true },
        { title: '事业部', dataIndex: 'field5', sorter: true },
        { title: '省份', dataIndex: 'field6', sorter: true },
        { title: '城市', dataIndex: 'field7', sorter: true },
        { title: '县区', dataIndex: 'field8', sorter: true }
      ];
    },
    //保存
    saveDialog() {
      this.$emit('close', { visible: false });
    },
    //取消操作
    closeDialog() {
      this.$emit('close', { visible: false });
    }
  },
  mounted() {
    this.initProvince();
  },
  created() {
    //获取事业部下拉列表
    this.getDeptData();
  }
};
</script>

<style lang="less" scoped>
</style>
